package com.squareup.picasso;

import android.graphics.drawable.Drawable;

import com.squareup.picasso.Picasso.LoadedFrom;

import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;

public abstract class Action<T> {
    final static class RequestWeakReference<T> extends WeakReference<T> {
        final Action action;

        public RequestWeakReference(Action action, T reference, ReferenceQueue queue) {
            super(reference, queue);
            this.action = action;
        }
    }

    final Picasso picasso;
    final Request data;
    final WeakReference<T> target;
    final int errorResId;
    final String key;
    final Object tag;
    boolean willReplay;
    boolean canceled;

    Action(Picasso picasso, T target, Request data, int errorResId, String key) {
        this.picasso = picasso;
        this.data = data;
        this.target = target == null?null:new RequestWeakReference(this, target, picasso.referenceQueue);
        this.errorResId = errorResId;
        this.key = key;
        this.tag = this;
    }

    abstract void complete(Drawable result, LoadedFrom from);

    abstract void error(Exception e);

    public void cancel() {
        this.canceled = true;
    }

    T getTarget() {
        return this.target == null ? null : this.target.get();
    }
}

